package com.morder.activity;

import android.app.Activity;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.morder.R;

public class RegistrationActivity extends Activity {
  private LocationClient mLocationClient = null;
  private BDLocationListener myListener = new MyLocationListener();
  private TextView mLocation;

  protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.registration_activity);

    mLocation = (TextView) findViewById(R.id.location);
    mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
    mLocationClient.registerLocationListener(myListener); // 注册监听函数

    initLBS();
  };

  public class MyLocationListener implements BDLocationListener {
    @Override
    public void onReceiveLocation(BDLocation location) {
      if (location == null)
        return;
      if (location.getLocType() == BDLocation.TypeGpsLocation) {
        System.out.println("location.getSpeed(): " + location.getSpeed());
        System.out.println("location.getSatelliteNumber()" + location.getSatelliteNumber());
      } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
        System.out.println("location.getAddrStr(): " + location.getAddrStr());
        mLocation.setText(location.getAddrStr() + "\n" + location.getLatitude() + " , " + location.getLongitude());
      }
      System.out.println("location.getLatitude(): " + location.getLatitude());
      System.out.println("location.getLongitude(): " + location.getLongitude());

      mLocationClient.stop();
    }

    public void onReceivePoi(BDLocation poiLocation) {
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    mLocationClient.start();
    mLocationClient.requestLocation();
  }

  @Override
  protected void onPause() {
    super.onPause();
    mLocationClient.stop();
  }

  private void initLBS() {
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true);
    option.setAddrType("all");// 返回的定位结果包含地址信息
    option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
    option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
    option.disableCache(true);// 禁止启用缓存定位
    option.setPoiNumber(5); // 最多返回POI个数
    option.setPoiDistance(1000); // poi查询距离
    option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
    mLocationClient.setLocOption(option);
  }
}
